package com.fw.service.message.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.bean.EntryBean;
import com.fw.service.message.MessageService;

public class MessageServiceImpl implements MessageService {

    // private static Logger log = Logger.getLogger(MessageServiceImpl.class.getName());

    @Autowired
    private org.springframework.context.support.custom.ReloadableResourceBundleMessageSource messageSource;

    @Override
    public String getMessage(String code, Locale locale) {
	return messageSource.getMessage(code, null, locale);
    }

    @Override
    public String getMessageWithDefaultMessage(String code, String defaultMessage, Locale locale) {
	return messageSource.getMessage(code, null, defaultMessage, locale);
    }

    @Override
    public String getMessageWithParams(String code, Object[] args, Locale locale) {
	return messageSource.getMessage(code, args, locale);
    }

    @Override
    public String getMessageWithParamsWithDefaultMessage(String code, Object[] args, String defaultMessage, Locale locale) {
	return messageSource.getMessage(code, args, defaultMessage, locale);
    }

    @Override
    public List<EntryBean<String, String>> getMessageAll(Locale locale) {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	Properties properties = messageSource.getProperties(locale);
	for (Object key : properties.keySet()) {
	    entryBeans.add(new EntryBean<String, String>(key.toString(), properties.getProperty(key.toString())));
	}
	return entryBeans;
    }
}
